package jp.co.hcs.attendancemanagement.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * The Class MD5Encrypt.
 */
public class MD5Encrypt {

  /**
   * Gets the string encrypt.
   * 
   * @param str the str
   * @return the string encrypt
   * @throws NoSuchAlgorithmException the no such algorithm exception
   * @throws UnsupportedEncodingException the unsupported encoding exception
   */
  public static String getStringEncrypt(String str) throws NoSuchAlgorithmException,
      UnsupportedEncodingException {
    final StringBuilder sbMd5Hash = new StringBuilder();
    final MessageDigest m = MessageDigest.getInstance("MD5");
    m.update(str.getBytes("UTF-8"));

    final byte data[] = m.digest();

    for (byte element : data) {
      sbMd5Hash.append(Character.forDigit((element >> 4) & 0xf, 16));
      sbMd5Hash.append(Character.forDigit(element & 0xf, 16));
    }

    return sbMd5Hash.toString();
  }

}
